package com.ylsk.web.controller.system;

import com.alibaba.fastjson.JSONObject;


import com.google.common.collect.Maps;
import com.ylsk.common.constant.Constants;
import com.ylsk.common.core.domain.AjaxResult;
import com.ylsk.common.core.domain.entity.SysUser;
import com.ylsk.common.enums.UserStatus;
import com.ylsk.common.exception.base.BaseException;
import com.ylsk.common.utils.StringUtils;
import com.ylsk.framework.web.service.SysLoginService;
import com.ylsk.system.service.ISysPostService;
import com.ylsk.system.service.ISysRoleService;
import com.ylsk.system.service.ISysUserService;

import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 第三方登录验证
 *
 * @author ruoyi
 */
//@CrossOrigin
@RestController
public class ThirdPartLoginController {
    protected final Logger log = LoggerFactory.getLogger(this.getClass());
    /**
     * @Description: 平台带着token来系统里面登陆
     * 这边需要做两个步骤：
     * 1.检测数据库里面有没有这个用户名，有则不操作，无则添加
     * 2.去平台验证一下Token是否有，有的话继续操作后面的登录
     * 平台没有这个token，则直接打回去，不让上来
     * @author: 穆雄雄
     * @date: 2022/4/19 上午 11:38
     * @Return: com.ruoyi.common.core.domain.AjaxResult
     */
    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysPostService postService;
    @Autowired
    private SysLoginService loginService;

    @PostMapping("/toThirdPartGetAuthJHaveToken")
    @ApiOperation(value = "平台带着token过来登录")
    public AjaxResult toThirdPartGetAuthJHaveToken(String token) {

//        SysUser sysUser = userService.selectUserByUserName(token);
//        if (StringUtils.isNull(sysUser)) {
//            log.info("登录用户：{} 不存在.", token);
//            throw new UsernameNotFoundException("登录用户：" + token + " 不存在");
//        } else if (UserStatus.DELETED.getCode().equals(sysUser.getDelFlag())) {
//            log.info("登录用户：{} 已被删除.", token);
//            throw new BaseException("对不起，您的账号：" + token + " 已被删除");
//        } else if (UserStatus.DISABLE.getCode().equals(sysUser.getStatus())) {
//            log.info("登录用户：{} 已被停用.", token);
//            throw new BaseException("对不起，您的账号：" + token + " 已停用");
//        }

        //返回结果
        Map<String,Object> result  = Maps.newHashMap();
        // 生成令牌(不加验证码登录)
        String tokenNew = loginService.jwtLogin(token);
        result.put(Constants.TOKEN, tokenNew);
        result.put("msg", "登录成功");
        return AjaxResult.success(result);
    }

    /**
     * 通过第三方平台接口，鉴定token合法性，并返回userName等登录信息
     * 这个方法需要根据实际需要进行修改
     * @param token
     * @return
     */
    public JSONObject checkJToken(String token) {
        return null;
//        JSONObject jsonObject = new JSONObject();
//        //测试环境
//        String baseUrl = "http://xxxxx/checkTokenRtnInfo?stk=" + token;//根据实际地址进行修改
//        HttpRequest request = HttpRequest.get(baseUrl)
//                .header(HttpHeaders.CONTENT_TYPE, "application/json")
//                .header(HttpHeaders.ACCEPT, "application/json");
//
//        HttpResponse response = request.send();
//        return (JSONObject) JSONObject.parse(response.bodyText());
    }
}
